<template>
  <Container class="full-gift-sale  single-sale">
    <div slot="header" class="container-header title title-center">满赠促销
      <Button size="large" class="fl btn-back" icon="ios-arrow-back" @click="$router.back()">返回</Button>
    </div>
    <!-- 基础设置 -->
    <DefaultEditItem ref="defaultEdit" :form="$store.state.fullGiftSale.form" />
    <div slot="footer" class="container-footer bg-white">
      <!-- <Button size="large" @click="jumpPrevPage">返回</Button> -->
      <Button type="primary" size="large" class="ml" @click="onClickNext">下一步</Button>
    </div>
  </Container>
</template>
<script>
import DefaultEditItem from './edit/DefaultEditItem'
export default {
  name: 'fullgift',
  components: {
    DefaultEditItem
  },
  data () {
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      isEmpty: false, // 是否清空数据
      isSave: true // 是否保存数据
    }
  },
  mounted () {
    // this.id && this.getDetailData()
  },
  methods: {
    getDetailData () {
      this.$ajaxGet('/activity/full_gift/detail/' + `${this.id}`).then(data => {
        this.$store.state.fullGiftSale.form.remarks = data.activityInfoDTO.activityDescription//活动备注
        this.$store.state.fullGiftSale.form.name = data.activityInfoDTO.activityName //促销名称
        this.$store.state.fullGiftSale.form.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList //活动对象
        this.$store.state.fullGiftSale.form.date = [new Date(data.activityInfoDTO.startDate), new Date(data.activityInfoDTO.endDate)]
        this.$store.state.fullGiftSale.form.term = data.activityFullGiftDTO.fullGiftTypeEnum == 'FULL_GIFT' ? 1 : 2 // 促销类型
        this.$store.state.fullGiftSale.form.discountGift = data.activityFullGiftDTO.fullGiftRuleList.map(item => {
          return {
            value: item.m,
            n: item.n,
            isParcel: item.isParcel
          }
        }) // 促销规则
        this.$store.state.fullGiftSale.form.discount1 = data.activityFullGiftDTO.fullGiftRuleList.map(item => {
          return {
            value: item.m,
            n: item.n,
            isParcel: item.isParcel
          }
        }) // 促销规则
        this.$store.state.fullGiftSale.form.discount2 = data.activityFullGiftDTO.fullGiftRuleList.map(item => {
          return {
            value: item.n,
            n: item.n
          }
        }) // 促销规则
      })
    },
    jumpPrevPage () {
      this.$router.back()
    },
    onClickNext () {
      let self = this
      this.$refs['defaultEdit'].$refs['form'].validate(function (validate) {
        if (validate) {
          let url = { name: 'sales/fullGiftSale/select', query: {} }
          self.$Message.success('保存成功!')
          if (self.id) {
            url.query.id = self.id
            if (self.type) url.query.type = self.type
          }
          self.$router.push(url)
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    /*
      1. 路由从不是下一页进入的都清空
      2. 路由从下一页进入并且有id就不刷新接口--保存信息
    */
    next(vm => {
      if (from.name !== 'sales/fullGiftSale/select') {
        vm.isEmpty = true
      } else {
        vm.isSave = false
      }
      if (vm.isEmpty) vm.$store.commit('setFullGiftSaleEmpty')
      if (vm.id && vm.isSave) vm.getDetailData()
    })
  }
}
</script>

